
package com.epam.android.training.day7;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author pavel_busko
 * 
 * Sample Service
 */
public class TestService extends Service {

    private static final int FIRST_BROADCAST_DELAY = 300;

    private static final int BROADCAST_PERIOD = 5000;

    private Timer timer;

    private Intent broadcastIntent;

    @Override
    public void onCreate() {
        super.onCreate();
        broadcastIntent = new Intent(this, TestReceiver.class);
        broadcastIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.broadcast_message));
        timer = new Timer();
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        TimerTask broadcastTask = new TimerTask() {
            @Override
            public void run() {
                sendBroadcast(broadcastIntent);
            }
        };
        timer.schedule(broadcastTask, FIRST_BROADCAST_DELAY, BROADCAST_PERIOD);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
